//
//  LVQuickPopUpView.m
//  LEVE
//
//  Created by 上官惠阳 on 2017/2/10.
//  Copyright © 2017年 dashuju. All rights reserved.
//

#import "LVSessionQuickPopUpView.h"
#import "LVSessionModel.h"
#import "FXBlurView.h"
#import "HYAnimationManager.h"
#import "UIView+Layer.h"

#define kTag 222222

@implementation LVSessionQuickAction
+ (LVSessionQuickAction *)actionWithImageName:(NSString *)imageName action:(dispatch_block_t)action
{
    LVSessionQuickAction *ac = [[LVSessionQuickAction alloc] initWithImageName:imageName action:action];
    return ac;
}
- (id)initWithImageName:(NSString *)imageName action:(dispatch_block_t)action
{
    self = [super init];
    if (self) {
        _imageName = imageName;
        _action = action;
    }
    return self;
}
@end

@interface  LVSessionQuickPopUpView ()
@property (strong, nonatomic) LVSessionModel *sessionModel;
@property (strong, nonatomic)LVSessionQuickAction *rightAction;
@property (strong, nonatomic)NSArray *actions;
@property (strong, nonatomic)FXBlurView *blurView;
@property (strong, nonatomic)UIView *contentView;
@property (strong, nonatomic)UIImageView *headIconView;
@property (strong, nonatomic)UILabel *nameLabel;
@property (strong, nonatomic)UILabel *numberabel;
@property (strong, nonatomic)UILabel *relationshipLabel;
@property (strong, nonatomic) UIView *line;
@property (strong, nonatomic) UIView *btnLine;
@property (strong, nonatomic)UIButton *rightBtn;
@property (strong, nonatomic)NSArray *btnArr;
@property (strong, nonatomic)UIView *btnView;
@end
@implementation LVSessionQuickPopUpView
+ (BOOL)alertIsShowingInView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    UIView *alert = [view viewWithTag:kTag];
    return alert != nil;
}
- (instancetype)initWithSessionModel:(LVSessionModel *)sessionModel rightAction:(LVSessionQuickAction *)rightAction actions:(NSArray <LVSessionQuickAction *>*)actions
{
    if (self = [super init]) {
        _sessionModel = sessionModel;
        _rightAction = rightAction;
        _actions = actions;
        [self setUp];
    }
    return self;
}
- (void)setUp
{
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];

    _blurView = [[FXBlurView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    _blurView.tintColor = [UIColor clearColor];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissFromSuperview)];
    [self addGestureRecognizer:tap];

    _headIconView = [[UIImageView alloc] init];
    _headIconView.backgroundColor = KOrangeColor;
    _headIconView.userInteractionEnabled = YES;
    [self addSubview:_headIconView];

    _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_rightBtn setImage:[UIImage imageNamed:_rightAction.imageName] forState:UIControlStateNormal];
    [_rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:_rightBtn];

    _nameLabel = [[UILabel alloc] init];
    _nameLabel.textColor = [UIColor blackColor];
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.font = [UIFont systemFontOfSize:18];
    [self.contentView addSubview:_nameLabel];

    _numberabel = [[UILabel alloc] init];
    _numberabel.textColor = KGrayColor;
    _numberabel.textAlignment = NSTextAlignmentRight;
    _numberabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_numberabel];

    _relationshipLabel = [[UILabel alloc] init];
    _relationshipLabel.textColor = KGrayColor;
    _relationshipLabel.textAlignment = NSTextAlignmentLeft;
    _relationshipLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:_relationshipLabel];

    _line = [[UIView alloc] init];
    _line.backgroundColor = KLightGrayColor;
    [self.contentView addSubview:_line];

    _btnLine = [[UIView alloc] init];
    _btnLine.backgroundColor = KLightGrayColor;
    [self.contentView addSubview:_btnLine];

    _btnView = [[UIView alloc] init];
    [self.contentView addSubview:_btnView];

    NSMutableArray *mBtnArr = [[NSMutableArray alloc] init];
    for (int i = 0; i < _actions.count; i ++) {
        LVSessionQuickAction *action = _actions[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:[UIImage imageNamed:action.imageName] forState:UIControlStateNormal];
        btn.tag = kTag + i;
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_btnView addSubview:btn];
        [mBtnArr addObject:btn];
    }
    _btnArr = mBtnArr;

    [self loadPopViewWithModel:_sessionModel];
}
- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat conWidth = (kScreenWidth == 320) ? 168 : 188;
    [self.contentView setFrame:CGRectMake(0, 0, kScreenWidth - 80, conWidth)];
    self.contentView.centerX = self.centerX;
    self.contentView.centerY = self.centerY;

    _headIconView.width = 90;
    _headIconView.height = 90;
    _headIconView.top = self.contentView.top - 45;
    _headIconView.centerX = self.contentView.centerX;
    [_headIconView makeRoundedCorners];
    [_headIconView makeBorderColor:KLightGrayColor borderWidth:5];
    [self bringSubviewToFront:_headIconView];

    _rightBtn.width = 23;
    _rightBtn.height = 21;
    _rightBtn.top = 18;
    _rightBtn.left = self.contentView.width - 18 - 23;

    _nameLabel.width = self.contentView.width - 60;
    _nameLabel.height = 25;
    _nameLabel.centerX = self.contentView.width / 2;
    _nameLabel.centerY = self.contentView.height / 2 - 20;

    _line.width = 0.8;
    _line.height = 9;
    _line.centerX = _nameLabel.centerX;
    _line.top = _nameLabel.bottom + 10;

    _numberabel.width = self.contentView.width / 2 - 40;
    _numberabel.height = 18;
    _numberabel.centerY = _line.centerY;
    _numberabel.right = _line.left - 20;

    _relationshipLabel.width = _numberabel.width;
    _relationshipLabel.height = _numberabel.height;
    _relationshipLabel.left = _line.right + 20;
    _relationshipLabel.centerY = _line.centerY;

    _btnLine.width = self.contentView.width;
    _btnLine.left = 0;
    _btnLine.height = 0.8;
    _btnLine.top = self.contentView.height - 53;

    _btnView.width = self.contentView.width - 40;
    _btnView.height = 52;
    _btnView.top = _btnLine.bottom + 1;
    _btnView.centerX = self.contentView.width / 2;

    for (int i = 0 ; i < _btnArr.count; i ++) {
        UIButton *btn = _btnArr[i];
        btn.width = _btnView.width / _btnArr.count;
        btn.height = 52;
        btn.left = btn.width * i;
        btn.top = 0;
    }
    [self loadPopViewWithModel:_sessionModel];
}
- (void)loadPopViewWithModel:(LVSessionModel *)model
{
    _nameLabel.text = @"William";
    _numberabel.text = @"Leve1088";
    _relationshipLabel.text = @"friend";
}
- (void)rightBtnAction:(UIButton *)btn
{
    if (_rightAction.action) {
        _rightAction.action();
        [self dismissFromSuperview];
    }
}
- (void)btnAction:(UIButton *)btn
{
    LVSessionQuickAction *action = _actions[btn.tag - kTag];
    if (action.action) {
        action.action();
        [self dismissFromSuperview];
    }
}
- (UIView *)contentView {
    if (_contentView == nil) {
        UIView *contentView = [[UIView alloc] init];
        [self addSubview:contentView];
        _contentView = contentView;
        contentView.layer.cornerRadius = 13.f;
        contentView.backgroundColor = [UIColor whiteColor];
    }
    return _contentView;
}
- (void)showInView:(UIView *)view {

    BOOL isShowing = [LVSessionQuickPopUpView alertIsShowingInView:view];
    if (isShowing) return ;

    UIView *content = view ? view  : [UIApplication sharedApplication].keyWindow;
    _blurView.blurRadius = 0;
    self.frame = content.bounds;
    self.alpha = 0;
    self.tag = kTag;

    [content addSubview:_blurView];
    [content addSubview:self];

    [HYAnimationManager addTransformAnimationForView:self.contentView];

    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = 1;
        _blurView.blurRadius = 12.0f;
    } completion:^(BOOL finished) {
        _blurView.dynamic = NO;
        [FXBlurView setUpdatesDisabled];
    }];
}
//移除掉
- (void)dismissFromSuperview
{
    [UIView animateWithDuration:0.35 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        if (finished) {
            [self removeFromSuperview];
            [_blurView clearImage];
            [_blurView removeFromSuperview];
        }
    }];
}
@end
